<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="deleteSpecs()" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="listSpecifications()"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input id="selectByName">
									<button class="btn btn-default" onclick="selectByName()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
								  <tbody id="specification">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<form id="page">

								<a href="javascript:toPage(1)">首页</a>
								<a href="javascript:toPage(2)">上一页</a>
								第<input type="text"  id="pageNum" size="1px" readonly="readonly">页/
								共<input type="text" id="pages" size="1px" disabled="disabled">页/
								共<input type="text" id="total" size="1px" disabled="disabled">条
								<a href="javascript:toPage(3)">下一页</a>
								<a href="javascript:toPage(4)">尾页</a>
							</form>
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑窗口   新增 -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input  class="form-control" name="specName" placeholder="规格名称" >  </td>
		      	</tr>
			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" class="btn btn-default" title="新建" onclick="addOption()" ><i class="fa fa-file-o"></i> 新增规格选项</button>
                  
             </div>
			 
			 <table class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                        <tr>
				      <th class="sorting">规格选项</th>
				      <th class="sorting">排序</th>																
                      <th class="sorting">操作</th>	
                    </thead>
                    <tbody id="addSpec">
                      <tr>
				            <td>
				            	<input  class="form-control" name="optionName" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" name="orders" placeholder="排序">
				            </td>
							<td>
								<button type="button" class="btn btn-default" onclick="deleteOptions(this)" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <tr>
				            <td>
				            	<input  class="form-control" name="optionName" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" name="orders" placeholder="排序">
				            </td>
							<td>
								<button type="button" class="btn btn-default" onclick="deleteOptions(this)" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <tr id="addOption">
				            <td>
				            	<input  class="form-control"  name="optionName" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" name="orders" placeholder="排序">
				            </td>
							<td>
								<button type="button" class="btn btn-default" onclick="deleteOptions(this)" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
                    </tbody>
			  </table>
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addSpecification()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 编辑窗口   修改 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3>规格编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>规格名称</td>
						  <td><input  class="form-control" id="specName" name="specName" placeholder="规格名称" >  </td>
						  <td><input  class="form-control" id="specId" type="hidden" name="id" placeholder="规格名称" >  </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="新建" onclick="updateOption()" ><i class="fa fa-file-o"></i> 新增规格选项</button>

				  </div>

				  <table class="table table-bordered table-striped table-hover dataTable">
					  <thead>
					  <tr>
						  <th class="sorting">规格选项</th>
						  <th></th>
						  <th class="sorting">排序</th>
						  <th></th>
						  <th class="sorting">操作</th>
					  </thead>
					  <tbody id="updateSpec">

					  </tbody>
				  </table>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateSpecification()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>
<script>
	$(function (){
		listSpecifications()
	})
	function listSpecifications(num){
		$.ajax({
			url: "/specification/listSpecifications",
			data: {pageNum:num,name:$("#selectByName").val()},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				console.log(result)
				if(result.code == "666"){
					var s = result.data.rows
					var html = ""
					for (var i = 0; i < s.length; i++) {
						html += '<tr>'
						html += '<td><input type="checkbox" value="' + s[i].id + '"></td>'
						html += '<td>' + s[i].id + '</td>'
						html += '<td>' + s[i].specName + '</td>'
						html += '<td className="text-center">'
						html += '<button type="button" className="btn bg-olive btn-xs" data-toggle="modal" onclick="toUpdate(' + s[i].id + ')" data-target="#updateModal">修改</button>'
						html += '</td>'
						html += '</tr>'
					}
					$("#specification").html(html);
					// 分页
					$("#pageNum").val(result.data.pageNum);
					$("#pages").val(result.data.pages);
					$("#total").val(result.data.total);
				}else{
					$("#specification").html("<tr><td>" + result.message + "</td></td></tr>");
				}

			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	//模糊查询方法
	function selectByName(){
		listSpecifications()
	}
	//分页
	function toPage(id){
		var num = parseInt($("#pageNum").val());
		var pages = parseInt($("#pages").val());
		var p = num;
		if (id == 1) {
			p = 1;
			$("#pageNum").val(1);
		}
		if (id == 2){
			if (num == 1) {
				alert("已经到达首页了");
				return;
			}
			p = num - 1;
			$("#pageNum").val(num - 1);
		}
		if (id == 3){
			if (num == pages) {
				alert("已经到达尾页了");
				return;
			}
			p = num + 1;
			$("#pageNum").val(num + 1);
		}
		if (id==4){
			p = pages;
			$("#pageNum").val(pages);
		}

		listSpecifications(p);
	}
	//新增规格选项
	function addOption() {
		var cloneOption = $("#addOption").clone();
		$("#addSpec").append(cloneOption);
		$(cloneOption).find(":input").val('');
	}
	//修改规格选项
	function updateOption() {
		var cloneOption = $("#updateOption").clone();
		$("#updateSpec").append(cloneOption);
		$(cloneOption).find(":input").val('');
	}
	//删除规格选项
	function deleteOptions(now){
		$(now).parent().parent().remove();
	}
	//添加方法
	function addSpecification(){
		var specOptions = [];//定义一个数组
		var specName = $("[name=specName]").val()
		var specOption = $("#addSpec").find("tr") //获取整个表格的tr
		if(specName == null || name == ''){
			alert("名字不能为空")
			return;
		}
		$(specOption).each(function (i,e){  //循环tr
			var td = $(e).find("td"); //td = tr中的td
			var optionName = $(td[0]).find("input").val();//获取input框中的值
			var optionOrder = $(td[1]).find("input").val();
			var options = {optionName:optionName,orders:optionOrder};//按照json进行赋值
			specOptions.push(options);//添加到数组
		})
		var jsonSpec = JSON.stringify(specOptions)//转成string格式
		$.ajax({
			url: "/specification/addSpec",
			data: {specName:specName,SpecOption:jsonSpec},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == "666"){
					alert(result.message)
					listSpecifications()
				}else {
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})

	}
	//回想
	function toUpdate(id){
		$.ajax({
			url: "/specification/toUpdate",
			data: {id:id},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == "666"){
					$("#specName").val(result.data.specName);
					$("#specId").val(result.data.id);
					var ops = result.data.optionList;
					var html = ""
					for (var i = 0; i < ops.length; i++) {
						html += '<tr id="updateOption">'
						html += '<td>'
						html += '<input className="form-control" type="hidden" value="' + ops[i].id + '" name="optionName" placeholder="规格选项">'
						html += '</td>'
						html += '<td>'
						html += '<input className="form-control" value="' + ops[i].optionName + '" name="optionName" placeholder="规格选项">'
						html += '</td>'
						html += '<td>'
						html += '<input className="form-control" type="hidden" value="' + ops[i].specId + '" name="orders">'
						html += '</td>'
						html += '<td>'
						html += '<input className="form-control" value="' + ops[i].orders + '" name="orders" placeholder="排序">'
						html += '</td>'
						html += '<td>'
						html += '<button type="button" className="btn btn-default" onClick="deleteOptions(this)" title="删除"><i className="fa fa-trash-o"></i> 删除 </button>'
						html += '</td>'
						html += '</tr>'


					}
					$("#updateSpec").html(html)
				}else {
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	//修改
	function updateSpecification(){
		var arr = [];
		var specName = $("#specName").val()
		var id = $("#specId").val()
		var specOption = $("#updateSpec").find("tr")
		if(specName == null || specName == ''){
			alert("名字不能为空")
			return;
		}
		$(specOption).each(function (i,e){
			var td = $(e).find("td");
			var optionId = $(td[0]).find("input").val();
			var name = $(td[1]).find("input").val();
			var specId = $(td[2]).find("input").val();
			var orders = $(td[3]).find("input").val();
			var options = {id:optionId,optionName:name,specId:specId,orders:orders};
			arr.push(options);
		})
		var jsonSpec = JSON.stringify(arr)
		$.ajax({
			url: "/specification/updateSpec",
			data: {specName:specName,id:id,SpecOption:jsonSpec},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == "666"){
					alert(result.message)
					listSpecifications()
				}else {
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	//批量删除
	function deleteSpecs(){
		if(confirm("是否删除")){
			var ids = []  //定义一个数组
			$("[type=checkbox]").each(function (i, e) {   //获取所有复选框的value,循环
				if ($(e).prop("checked")) {   //判断是否被选中
					ids.push($(e).val())  //选中就添加到数组中
				}
			})
			$.ajax({
				url: "/specification/deleteSpecs?ids=" + ids,
				type: "get",
				dataType: "json",
				async: false,
				success: function (result) {
					if (result.code == "666") {
						alert(result.message)
						listSpecifications();
					} else {
						alert(result.message)
					}
				},
				error: function (){
					alert("系统异常")
				}
			})
		}
	}
</script>
</html>